<?php
class Teacher
{
	private $id;
	private $name;
	private $profession;

    /**
    * 一个教师教授多门课程
    *
    * @var Course
    */
	private $courses = [];

	public function __construct($id,$name,$profession)
	{
		$this->id = $id;
		$this->name = $name;
		$this->profession = $profession;
	}

	public function addCourse(Course $course)
	{
		$this->courses[] = $course;
		$course->teacher = $this;
	}

	public function introduce()
	{
		echo '我是' . $this->name . '我负责的课程是' .
		foreach ($this->courses as $course) {
			echo $course->name . '&nbsp;&nbsp;';
		}
	}

	public function __get($name)
	{
		if (property_exists(self::class,$name)) {
			return $this->name;
		}
		return null;
	}

	// 魔术方法，$name为属性名 $value为属性值
	public function __set($name,$value)
	{
		if (property_exists(self::class, $name)) {
		    $this->name = $value;
		}
	}
	public function __tostring()
	{
		return 'id' . $this->id . '姓名' . $this->name . '专业' . $this->profession . '<br/>';
	}
}